package com.qlslylq.ad.sdk.enums;

import com.qlslylq.ad.sdk.core.rsp.AdError;
import com.qlslylq.ad.sdk.util.GsonUtils;

import java.util.LinkedHashMap;

/**
 * Http错误码<br/>
 */
public enum ErrorEnum {

    //服务端定义
    ERR_SUCCESS(200, "SUCCESS"),
    //客户端自定义
    ERR_CONN(440, "客户端网络没有打开，请检查"),
    ERR_CONN_TIME_OUT(441, "请求超时"),
    ERR_SOCKET_TIME_OUT(442, "响应超时"),
    ERR_JSON_SYNTAX(443, "数据格式非json"),
    ERR_NET_OTHER(444, "其它类型网络异常"),
    //客户端自定义(聚合SDK执行链错误定义)
    ERR_INIT_DY_SDK(450, "获取数据失败，请稍后再试"),
    ERR_INIT_INVALID_ARG(451, "AppId或AppKey无效"),
    ERR_INIT_THIRD_SDK(452, "联盟SDK初始化失败"),
    ERR_REMOTE_NO_POS_AD_CONFIGS(453, "本地及远端均无广告位配置数据"),
    ERR_NO_AD(454, "暂无可用的广告，请重新请求"),
    ERR_VIDEO_PLAY(455, "播放异常，请重新请求"),
    ERR_RENDER(456, "渲染失败，请重新请求"),
    ERR_LOAD_FAILD(457, "加载失败"),
    ERR_LOAD(458, "加载异常"),
    ERR_SHOW_FAILD(459, "展示失败"),
    ERR_SHOW(460, "展示异常"),
    ERR_NOT_READY(461, "未准备就绪，还不能展示"),
    ERR_AD_INVALID(462, "该广告已失效，请重新请求"),
    ERR_SHOW_SPLASH(463, "请初始化含有adContainer的构造器来展示；不含adContainer的构造器仅用于预加载，不能用于展示"),
    ERR_SHOW_SPLASH_NEED_DELAY(464, "请在initSDK的success之后或延时合适的时间直到success后再load开屏广告"),
    //客户端自定义(聚合SDK执行链错误定义)-Web
    ERR_FAILD(480, "FAILD"),
    ERR_CONTEXT_NOT_MATCH_ACTIVITY(481, "context必须为activity类型"),
    ERR_NOT_INIT_AD_LOADER(482, "请先初始化广告加载器");

    private int id;

    private String name;

    ErrorEnum(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static ErrorEnum getEnumById(int id) {
        for (ErrorEnum item : ErrorEnum.values()) {
            if (item.getId() == id) {
                return item;
            }
        }
        return null;
    }

    /**
     * 是否为无效数据<br/>
     */
    public static boolean isDataInvalid(int id) {
        return id != ERR_SUCCESS.id;
    }

    /**
     * 模型化<br/>
     */
    public AdError rsp(AdPlatformEnum platform, AdType adType) {
        return AdError.build(platform, adType, id, name);
    }

    /**
     * 模型化(携带后缀指示数据)<br/>
     */
    public AdError rsp(AdPlatformEnum platform, AdType adType, LinkedHashMap<String, Object> extraInfo) {
        String suffix = GsonUtils.gsonString(extraInfo);
        return AdError.build(platform, adType, id, name + "：" + suffix);
    }

}
